<?

/*	Media.file OBJECT
*	Comment here
*
*					*/


class MediaFile extends MOD {

	function __construct( $vars ) {
		$this->_addvars( $vars );
		$this->_ref(); 
	
	}
	
	public function construct() {
		global $DB, $Page, $Object;

		$Object = new OBJECT( $this->vars->type, (int) $_GET['id'] );
		
		$Page->set_cache( "title", $Object->get( 'name' ));
		
	}
	
	function output () {
		
		// wrong:
		/*
		if( $this->action )
			$result = $this->{ "filter_" . $this->action }();
			
		return $this->parse( $result );
		*/
		
		// right:
		// as new addition to parse(), if content parameter is array,
		// it's transformed to an (newline separated) unsorted list automagically.
		if(	$this->action
		&&	$result = $this->{ "filter_" . $this->action }())
		
			return $this->parse( $result );
				
		
	}
	
/*	PARSE FUNCTIONALITIES
*	parse_symbols builds a symbol visualised file(s) overview, 
*	based on mod vars
*
*					*/

	function filter_single () {
		global $DB;
		
		if( $symbol = $DB->get( $this->vars->objtype, (int) $this->vars->id )) {
				
			$raw = json_decode( $symbol['meta'] )->raw;
			$symbol['type'] = $this->vars->objtype;
			if( !$symbol['nameid'] ) $symbol['name'] = str_replace('+', ' ', $raw);
			$symbol['file'] = $symbol['ID'] .".". urlencode( $raw ) .".". date( "Ymd", strtotime( $symbol['llog'] )) . "." . $this->vars->objtype;    
			$node = $this->render_symbol( $symbol );
		}		
		
		return ( $node )? $node: "empty.";
	}

	
	function filter_group_symbols () {
		global $DB;
		
		if( $group = $DB->reference( "group.file", "parentid=". $this->vars->id, $this->vars->objtype ))
			if( $symbols = $DB->filter( $this->vars->objtype, "ID in(". implode( ",", $group ) .")" ))
				foreach( $symbols as $symbol ) {

					$meta = json_decode( $symbol['meta'] );
					$symbol['type'] = $this->vars->objtype;
					if( !$symbol['nameid'] ) $symbol['name'] = str_replace('+', ' ', $meta->raw);
					$symbol['file'] = $symbol['ID'] .".". urlencode($meta->raw) .".". date( "Ymd", strtotime( $symbol['llog'] )) . "." . $this->vars->objtype;    
					$nodes[] = $this->render_symbol( $symbol );
				}
		
		return ( $nodes)? implode( "\n", $nodes ): "empty.";
	}
	
	private function render_symbol( $node ) {

		return "<a href='/DB/files/{$node['file']}' title=''><div class='symbol {$node['type']}'><span class='name'>{$node['name']}</span></div></a>";
	}


	/** 
	 * 	new frontend file rendering
	 *			
	 * 	extends the file upload in the engine (group and single files)
	 *	renders files with filename and class 'symbol' and extension of the file (example: 'pdf')
	 *	
	 *	@return		link for single file / list for multiple files
	 */
	 
#	render a single file	
	private function filter_file() {
		global $DB;
		
		// check if file exist
		if ( $file = $DB->filter_objects( 'media.file', 'ID = ' . $this->vars->fileid ))
			$file = $file[0];
			$node = $this->render_link( $this->vars->fileid, $file->name, $this->vars->type, $this->vars->type );
				
		return ( $node ) ? $node : 'empty.';
	}
	
# 	render multiple files in a group 
	private function filter_group() {
		global $Engine, $DB, $Object;
		
		if ( $group =  $DB->reference( 'group.file', 'parentid = ' . $this->vars->groupid, 'media.file' )) { 
		
			$info = $DB->get_object( 'group.file', 'ID = ' . $this->vars->groupid );
			$description = $Engine->text->get( $info->get( 'textid' ), 'long' );
			
			if( $description && $description != '...' )
				$nodes[] = '<p class="group_descr">' . $description . '</p>';
			
			$nodes[] = '<ul>';
			foreach ( $group as $file ) {
				
				$file 	= $DB->filter_objects( 'media.file', 'ID = ' . $file );
				$file 	= $file[0];
				
				if( is_object(  $file )) {
					$type 	= array_pop( explode( '.', $file->get('meta')->raw ));
					$descr	= $Engine->text->get( $file->get( 'textid' ), 'long' );

					if( $descr && $descr != '...' )
						$nodes[] = '<li>' . $this->render_link( $file->id, $file->get( 'name' ), $type, $descr ) . '</li>';
					
					else 
						$nodes[] = '<li>' . $this->render_link( $file->id, $file->get( 'name' ), $type ) . '</li>';
				}
			}
			$nodes[] = '</ul>';
		
		}
		
		return ( $nodes )? implode( "\n", $nodes ): '';
	}	
	
#	object media files
	private function filter_object() {
		global	$DB, $Object;
	
		if( $Object && $files = $Object->ref_objects( $this->vars->objtype, ( $this->vars->objtype )))
			foreach ( $files as $File )
				
				$nodes[] = $this->render_file( $File );
		return  $nodes;

	
		// Quite wrong:
		/*
		if ( $Object && $files = $DB->reference( $Object->type, $Object->get('ID'), 'media.file' )) { 
		
			$nodes[] = '<ul>';
			foreach ( $files as $file ) {
				
				$file 	= array_shift( $DB->filter_objects( 'media.file', 'ID = ' . $file ));
				$type 	= array_pop( explode( '.', $file->get('meta')->raw ));
				//$descr	= $Engine->text->get( $file->get( 'textid' ), 'long' );

				if( $descr && $descr != '...' )
					$nodes[] = '<li>' . $this->render_link( $file->id, $file->get( 'name' ), $type, $descr ) . '</li>';
					
				else 
					$nodes[] = '<li>' . $this->render_link( $file->id, $file->get( 'name' ), $type ) . '</li>';
			
			}
			$nodes[] = '</ul>';
		
		}
		
		return ( $nodes )? implode( "\n", $nodes ): '';*/
		
		
	}
	
	private function render_file( $Obj ) {
		
		$ext	= $Obj->get( "ext" )? $Obj->get( "ext" ): array_pop( explode( ".", $raw = $Obj->get( "raw" ))); 
		$file	= $Obj->get( $Obj->hasName()? "name": "raw" );
		$style	= $this->vars->icon? " style='background-image: url(". THEME . "img/{$this->vars->icon})'": "";
		
		return "<li><a href='/file.mod?id={$Obj->id}&type={$Obj->type}' title='$file'><div class='symbol $ext'$style>" . ( $this->vars->filename? "<span class='name'>$file</span>": "" ) . "</div></a></li>";
	}
	


#	return file & link
	private function render_link( $file, $name, $ext, $descr = false ) {
	
		$return = "<a href='/DB/media/" . $file . "." . $ext . "' title='" . $name . "'><div class='symbol " . $ext. "'><span class='name'>" . $name . "</span></div></a>";
		if( $descr )
			$return .= '<span class="descr">' . $descr . '</span>';
				
		return $return;
	}


} 

?>